

package com.hulk.dryad.admin.control;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hulk.dryad.admin.api.dto.UserExtDTO;
import com.hulk.dryad.admin.mapstruct.UserExtStruct;
import com.hulk.dryad.common.annotation.PrintlnLog;
import com.hulk.dryad.common.annotation.Idempotent;
import com.hulk.dryad.common.component.R;
import com.hulk.dryad.persistence.entity.SysUserModel;
import com.hulk.dryad.persistence.entity.UserExtModel;
import com.hulk.dryad.persistence.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author kim
 * @date 2018/12/16
 */
@ApiSort(4)
@Api(value = "user", tags = "用户管理模块")
@RestController
@AllArgsConstructor
@RequestMapping("/admin/user")
public class SysUserController {

	private final SysUserService userService;

	/**
	 * 获取指定用户全部信息
	 * @return 用户信息
	 */
	@ApiOperation(value = "user", notes = "获取指定用户全部信息")
	@ApiOperationSupport
	@PrintlnLog
	@GetMapping("/info/{username}")
	public R info(@PathVariable String username) {
		SysUserModel user = userService.getOne(Wrappers.<SysUserModel>query().lambda().eq(SysUserModel::getUsername, username));
		if (user == null) {
			return R.failed(null, String.format("用户信息为空 %s", username));
		}
		return R.ok(userService.findUserInfo(user));
	}

	/**
	 * 通过ID查询用户信息
	 * @param id ID
	 * @return 用户信息
	 */
	@ApiOperation(value = "id", notes = "通过ID查询用户信息")
	@ApiOperationSupport(order = 1)
	@PrintlnLog
	@GetMapping("/{id}")
	public R user(@PathVariable Integer id) {
		return R.ok(userService.selectUserVoById(id));
	}

	/**
	 * 根据用户名查询用户信息
	 * @param username 用户名
	 * @return
	 */
	@ApiOperation(value = "username", notes = "根据用户名查询用户信息")
	@ApiOperationSupport(order = 2)
	@PrintlnLog
	@GetMapping("/details/{username}")
	public R user(@PathVariable String username) {
		SysUserModel condition = new SysUserModel();
		condition.setUsername(username);
		return R.ok(userService.getOne(new QueryWrapper<>(condition)));
	}

	/**
	 * 删除用户信息
	 * @param id ID
	 * @return R
	 */
	@ApiOperation(value = "userDel删除用户", notes = "根据ID删除用户")
	@ApiOperationSupport(order = 3)
	@Idempotent
	@PrintlnLog
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_user_del')")
	@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int", paramType = "path")
	public R userDel(@PathVariable Integer id) {
		SysUserModel sysUser = userService.getById(id);
		return R.ok(userService.deleteUserById(sysUser));
	}

	/**
	 * 添加用户
	 * @param userDto 用户信息
	 * @return success/false
	 */
	@ApiOperation(value = "userAdd", notes = "添加用户")
	@ApiOperationSupport(order = 4)
	@Idempotent
	@PrintlnLog
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_user_add')")
	public R userAdd(@RequestBody UserExtDTO userDto) {
		UserExtModel model =UserExtStruct.INSTANCE.to(userDto);
		return R.ok(userService.saveUser(model));
	}

	/**
	 * 更新用户信息
	 * @param userDto 用户信息
	 * @return R
	 */
	@ApiOperation(value = "updateUser", notes = "更新用户信息")
	@ApiOperationSupport(order = 5)
	@Idempotent
	@PrintlnLog
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_user_edit')")
	public R updateUser(@Valid @RequestBody UserExtDTO userDto) {
		UserExtModel model =UserExtStruct.INSTANCE.to(userDto);
		return R.ok(userService.updateUser(model));
	}

	/**
	 * 分页查询用户
	 * @param page 参数集
	 * @param userDto 查询参数列表
	 * @return 用户集合
	 */
	@ApiOperation(value = "getUserPage", notes = "分页查询用户")
	@ApiOperationSupport(order = 6)
	@PrintlnLog
	@GetMapping("/page")
	public R getUserPage(Page page, UserExtDTO userDto) {
		UserExtModel model =UserExtStruct.INSTANCE.to(userDto);
		return R.ok(userService.getUsersWithRolePage(page, model));
	}

	/**
	 * 修改个人信息
	 * @param userDto userDto
	 * @return success/false
	 */
	@ApiOperation(value = "updateUserInfo", notes = "修改个人信息")
	@ApiOperationSupport(order = 7)
//	@Idempotent
//	@PrintlnLog
	@PutMapping("/edit")
//	@PreAuthorize("@pms.hasPermission('sys_user_edit')")
	public R updateUserInfo(@Valid @RequestBody UserExtDTO userDto) {
		UserExtModel model =UserExtStruct.INSTANCE.to(userDto);
		return R.ok(userService.updateUserInfo(model));
	}

	/**
	 * @param username 用户名称
	 * @return 上级部门用户列表
	 */

	@ApiOperation(value = "listAncestorUsers", notes = "获取上级部门用户列表")
	@ApiOperationSupport(order = 8)
	@PrintlnLog
	@GetMapping("/ancestor/{username}")
	public R listAncestorUsers(@PathVariable String username) {

		return R.ok(userService.listAncestorUsers(username));
	}

}
